from LoadLib import *
import random

n1 = noise(8,1.1)
n2  = noise(6,2.7) 
n3 = noise(5, 3.9)

checkTex = lambda p:C(1,0,0) if (floor(p.x * 10) + floor(p.y * 10) + floor(p.z * 10)) % 2 == 0 else C(0,1, 0)

red = lambda p : C(1,0,0)



def noiseTexture(p):
    p2 = P2(p.x, p.y)
    v1 = n1(p2)
    v2 = n2(p2)
    l = lerp(v1,v2,(p.z+1)/2)
    return C(frac(12*l),frac(50*l),frac(37*l))

def noiseTexture2(p):
    p2 = P2(p.x, p.y)
    v1 = n1(p2)
    v2 = n3(p2)
    l = lerp(v1,v2,(p.z+1)/2)
    return C(frac(12*l),frac(50*l),frac(37*l))

def noiseTexture3(p):
    p2 = P2(p.x, p.y)
    p3 = P2(p.x, p.z)
    p4 = P2(p.y, p.z)
    v1 = n1(p2)
    v2 = n2(p2)
    v3 = n3(p3)
    v4 = n3(p4)
    l = lerp(v1,v2,lerp(v1, v3,v4))
    return C(frac(12*l),frac(50*l),frac(37*l))

def woodTexture(p):
    r = abs(.1*P2(p.x, p.z))
    f = frac(r * 30)
    return C(1, .8, .5) if f < .8 else C(.5, .3 , 0)


def woodTexture2(p):
    r = abs(P2(p.x, p.y))
    noise1 = n2(P2(r/2, p.z))
    r1 =  r * (noise1 + 7)/11
    rand = random.randrange(1, 5, 1)
    f = frac(r1 * rand * 20)
    return C(1, .8, .5) if f < .8 else C(.5, .3 , 0)

def vortexlens(p):
    (r, theta) = toPolar(p)
    rmin = .7
    k = 3
    return fromPolar(r, theta + k*min(0, r-rmin))

def perlinlens(p):
    k = .5
    return P2(p.x + k*n1(p)-.5, p.y + k*n2(p) -.5)
    





simp = rayTracer(sphereGeom, red)

checkSphere = rayTracer(sphereGeom, checkTex, camera = P3(3,3,3))
checkCube = rayTracer(cubeGeom, checkTex, camera = P3(3,3,3))
checkcy = rayTracer(cylinderGeom, checkTex, camera = P3(2,0,3))

woodSphere = rayTracer(sphereGeom, woodTexture2, camera = P3(3,3,3))
woodCube = rayTracer(cubeGeom, woodTexture2, camera = P3(3,3,3))
woodcy = rayTracer(cylinderGeom, woodTexture |o| rotate(.5), camera = P3(-1,2,3))
woodcy2 = rayTracer(cylinderGeom, woodTexture , camera = P3(-1,-2,3))
woodcy3 = rayTracer(cylinderGeom, woodTexture2, focal = 2, camera = P3(-1,-2,-2))
woodSphere = rayTracer(sphereGeom, woodTexture2)

noisecy = rayTracer(cylinderGeom, noiseTexture, focal = 2, camera = P3(1,-2,-2))
noiseCube = rayTracer(cubeGeom, noiseTexture, camera = P3(3,3,-3)) 
noiseSphere = rayTracer(sphereGeom, noiseTexture)

noisecy2 = rayTracer(cylinderGeom, noiseTexture2, lightDir = P3(1,1,-1), focal = 2, camera = P3(1,2,-2))
noiseCube2 = rayTracer(cubeGeom, noiseTexture2,focal = 2, lightDir = P3(1,1,-1), camera = P3(3,3,-3)) 
noiseSphere2 = rayTracer(sphereGeom, noiseTexture2,lightDir = P3(1,1,-1), camera = P3(3,3,-3), focal = 4)

noiseCube3 = rayTracer(cubeGeom, noiseTexture3,focal = 3, lightDir = P3(1,1,-1), camera = P3(3,3,-3)) 
noiseSphere3 = rayTracer(sphereGeom, noiseTexture3,lightDir = P3(1,1,-1), camera = P3(3,3,-3), focal = 4)


imageSphere  =  lambda p: (rayTracer(sphereGeom, lambda p: z.image1(P2(p.x, .8*p.z)), lightDir = P3(1,1,1), camera = fromCyl(4, z.time, 0)))(p)
imageCube = lambda p:(rayTracer(cubeGeom, lambda p: (over(z.image1, lambda p:white)) (P2(lerp(p.y*.39,p.x*.39,p.y*.39), .39*p.z)), camera = fromCyl(4, z.time, 1), focal = 2, lightDir = P3(1,-1,1)))(p)
imagecyl = lambda p:(rayTracer(cylinderGeom, lambda p: (over(z.image1, lambda p:white)) (P2(lerp(p.y*.39,p.x*.39,p.y*.39), .39*p.z)), camera = fromCyl(4, z.time, 2), focal = 2, lightDir = P3(1,-1,1)))(p)
colorCube = rayTracer(cubeGeom, lambda p: C(p.x, p.y, p.z),camera = P3(3,3,3))